package com.yan.controller;

import com.yan.pojo.Student;
import com.yan.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
    @Qualifier("StudentServiceImpl")
    private StudentService studentService;
    @RequestMapping("/allStudent")
    public String list(Model model){
        List<Student> list=studentService.queryStudent();
        model.addAttribute("list",list);
        return "allStudent";
    }

    @RequestMapping("/toAddStudent")
    public String toAddStudent(){
        return "addStudent";
    }

    @RequestMapping("/addStudent")
    public String addStudent(Student student){
        studentService.addStudent(student);
        return "redirect:allStudent";

    }

    @RequestMapping("toUpdateStudent")
    public String toUpdateStudent(int id,Model model){
        Student student=studentService.queryStudentById(id);
        model.addAttribute("QStudent",student);
        return "updateStudent";

    }
    @RequestMapping("/updateStudent")
    public String updateStudent(Student student){
        System.out.println("updateStudent=>"+student);
        int i=studentService.updateStudent(student);
        if (i>0){
            System.out.println("修改成功"+student);
        }
        return "redirect:allStudent";
    }

}
